/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Engine2D.level.tile;

import Engine2D.entity.Entity;
import Engine2D.graphics.GLSpriteSheet;
import Engine2D.level.Level;
import Engine2D.graphics.GLImage;

/**
 *
 * @author Garys
 */
public class SandTile extends Tile {

    /**
     * 
     * @param id
     * @param spriteSheet
     * @param sizeX
     * @param sizeY
     */
    public SandTile(int id, GLSpriteSheet spriteSheet, int sizeX, int sizeY) {
        super(id, sizeX, sizeY);
        this.image = spriteSheet;
    }
    
    
    /**
     * 
     * @param level
     * @param x
     * @param y
     */
    @Override
    public void render(Level level, int x, int y, int xt, int yt) {
        image.drawTile(x, y,224f, 480f);
        
        
        Tile aboveT = level.getTile(xt, yt - 1);
        Tile belowT = level.getTile(xt, yt + 1);
        Tile leftT = level.getTile(xt - 1, yt);
        Tile rightT = level.getTile(xt + 1, yt);

        boolean above = aboveT != this;
        boolean below = belowT != this;
        boolean left = leftT != this;
        boolean right = rightT != this;

        Tile aboveLeftT = level.getTile(xt - 1, yt - 1);
        Tile belowLeftT = level.getTile(xt - 1, yt + 1);
        Tile aboveRightT = level.getTile(xt + 1, yt - 1);
        Tile belowRightT = level.getTile(xt + 1, yt + 1);


        boolean aboveLeft = aboveLeftT != this;
        boolean belowLeft = belowLeftT != this;
        boolean aboveRight = aboveRightT != this;
        boolean belowRight = belowRightT != this;

        if (above) {
            if (aboveT == TileManager.grass) {
                above = false;
            }
            else if (aboveT == TileManager.rock) {
                above = false;
            }
            else if (aboveT == TileManager.water) {
                image.draw(x, y, 32, 8, 96f, 480f, 1f);
                image.startBackground(32, 32, 1f);
            }
            
            else if(aboveT == TileManager.tunnel) {
                image.draw(x, y, 32, 8, 288f, 480f, 1f);
                image.startBackground(32, 32, 1f);
            }
        }
        if (below) {
            if (belowT == TileManager.grass) {
                below = false;
            }
            else if (belowT == TileManager.rock) {
                below = false;
            }
            else if (belowT == TileManager.water) {
                image.draw(x, y+24, 32, 8, 96f, 480f, 1f);
                image.startBackground(32, 32, 1f);
            }
            else if(belowT == TileManager.tunnel) {
                image.draw(x, y+24, 32, 8, 288f, 480f, 1f);
                image.startBackground(32, 32, 1f);
            }
        }
        if (left) {
            if (leftT == TileManager.grass) {
                left = false;
            }
            else if (leftT == TileManager.rock) {
                left = false;
            }
            else if (leftT == TileManager.water) {
                image.draw(x, y, 8, 32, 96f, 480f, 1f);
                image.startBackground(32, 32, 1f);
            }
            else if(leftT == TileManager.tunnel) {
                image.draw(x, y, 8, 32, 288f, 480f, 1f);
                image.startBackground(32, 32, 1f);
            }
        }
        if (right) {
            if (rightT == TileManager.grass) {
                right = false;
            }
            else if (rightT == TileManager.rock) {
                right = false;
            }
            else if (rightT == TileManager.water) {
                image.draw(x+24, y, 8, 32, 96f, 480f, 1f);
                image.startBackground(32, 32, 1f);
            }
            else if(rightT == TileManager.tunnel) {
                image.draw(x+24, y, 8, 32, 288f, 480f, 1f);
                image.startBackground(32, 32, 1f);
            }
        }
        
        
        
        

        if (above && below) {

            if (left && right) {
                image.drawTile(x, y, 160f, 384f);
            } else if (right) {
                image.drawTile(x, y, 96f, 352f);
            } else if (left) {
                image.drawTile(x, y, 128f, 352f);
            } else {
                image.drawTile(x, y, 224f, 384f);
            }

        } else if (above) {

            if (left && right) {
                image.drawTile(x, y, 128f, 384f);
            } else if (right) {
                if (below) {
                    image.drawTile(x, y, 96f, 352f);
                } else {
                    image.drawTile(x, y, 64f, 384f);
                }
            } else if (left) {
                if (below) {
                    image.drawTile(x, y, 128f, 352f);
                } else {
                    image.drawTile(x, y, 0f, 384f);
                }
            } else {
                image.drawTile(x, y, 32f, 384f);
            }

        } else if (below) {

            if (left && right) {
                image.drawTile(x, y, 96f, 384f);
            } else if (right) {
                if (above) {
                    image.drawTile(x, y, 96f, 352f);
                } else {
                    image.drawTile(x, y, 64f, 352f);
                }
            } else if (left) {
                if (above) {
                    image.drawTile(x, y, 128f, 416f);
                } else {
                    image.drawTile(x, y, 0f, 352f);
                }
            } else {
                image.drawTile(x, y, 32f, 352f);
            }


        } else {
            if (left && right) {
                image.drawTile(x, y, 192f, 384f);
            } else if (right) {
                image.drawTile(x, y, 192, 352f);
            } else if (left) {
                image.drawTile(x, y, 160f, 352f);
            } else {
                image.drawTile(x, y, 224f, 480f);
            }


        }
        
    }
    
    /**
     * 
     * @param aThis
     * @param x
     * @param y
     * @return
     */
    @Override
    public int getLightRadius(Level aThis, int x, int y) {
        return 1;
    }
    
}
